let lineHeight = 0
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hiddenLyric:{
      type:Boolean,
      value:false
    },
    lyric:String
  },
  observers:{
    lyric(val) {
      if (val) {
        this._formatData(val)
      } else {
          this.setData({
            lyricLists:[
              {
                text:'暂无歌词',
                time:-1
              }
            ]
          })
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    lyricLists:[],
    scrollTop:0,
    nowIndex:-1
  },
  lifetimes:{
    ready() {
      wx.getSystemInfo({
        success: (result) => {
          lineHeight = result.screenWidth / 750 * 64
        },
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 格式化数据
     */
    _formatData(str) {
      let arrs = str.split('\n')
      let lyricLists = []
      for(let i = 0; i< arrs.length ;i++) { 
        let item = arrs[i]
        let lineArrs = item.split(']')
        let strTime = lineArrs[0].split('[')[1]
        let text = lineArrs[1]
        
        let time = 0
        if (strTime) {
          let timeArrs = strTime.match(/(\d{2,}):(\d{2})(?:\.(\d{2,3}))?/)
          time = +timeArrs[1] * 60 + +timeArrs[2] + parseInt(+timeArrs[2] / 1000)
        }  
        if (text) {
          lyricLists.push({
            time,
            text
          })
        } 
      }
      this.setData({
        lyricLists
      })
    },
    /**
     * 时间变化更新歌词
     */
    updateTime(ev) {
      let time = ev.detail
      let lyricLists = this.data.lyricLists
      if (lyricLists.length > 0) {
        if (time > lyricLists[lyricLists.length - 1].time) {
          if (this.data.nowIndex !== -1) {
            this.setData({
              nowIndex: -1,
              scrollTop:lineHeight * lyricLists.length
            })
          }
        }
        for(let i = 0; i< lyricLists.length;i++) {
          let item = lyricLists[i]
          if (item.time >= time) {
            this.setData({
              nowIndex:i - 1,
              scrollTop:lineHeight * (i -1)
            })
            break;
          }
        }
      }
    }
  }
})
